package com.app.controller.admin;

import com.app.mapper.Information.MajorMapper;
import com.app.pojo.College;
import com.app.pojo.Major;
import com.app.pojo.Teacher;
import com.app.service.Information.CollegeService;
import com.app.service.Information.MajorService;
import com.app.service.teacher.TeacherService;
import com.app.tools.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    CollegeService collegeService;

    @Autowired
    MajorService majorService;

    @RequestMapping("showTeacher")
    public String showStudent(Model model, String name, Integer sex, String college,
                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        try {
        List<College> colleges = collegeService.getColleges();
        List<Teacher> teachers = teacherService.getLikeTeacher(name,sex,college);
        //分页
        PageInfo pageInfo = new PageInfo(pageNum,teachers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("colleges",colleges);
        model.addAttribute("teachers",teachers);
        }catch (Exception e){
            return "teacher/admin/showTeacher";
      }
        return "teacher/admin/showTeacher";
    }

    @RequestMapping("delTeacher")
    public String delTeacher(Model model,Integer id){
        int row = teacherService.delTeacher(id);
        if(row<1){
            model.addAttribute("error","删除时发送未知异常");
        }else {
            model.addAttribute("error","删除成功");
        }
        return showStudent(model,"",null,"",1);
    }

    @RequestMapping("toAddTeacher")
    public String toAddTeacher(Model model){
        List<College> colleges = collegeService.getColleges();
        model.addAttribute("colleges",colleges);
        return "teacher/admin/teacherAdd";
    }

    @RequestMapping("addTeacher")
    public String addTeacher(Model model,Teacher teacher,String loginId){
        int row = teacherService.addTeacher(teacher);
        if(row<1){
            model.addAttribute("error","添加时发生未知异常");
        }else {
            model.addAttribute("error","添加成功");
        }
        model.addAttribute("teacher",teacherService.getTeacherById(loginId));
        return toAddTeacher(model);
    }

    @RequestMapping("toUpdate")
    public String toUpdateTeacher(Model model,String teacherId){
        model.addAttribute("uTeacher",teacherService.getTeacherById(teacherId));
        model.addAttribute("colleges",collegeService.getColleges());
        return "teacher/admin/teacherUpdate";
    }

    @RequestMapping("teacherUpdate")
    public String teacherUpdate(Model model,Teacher teacher,String loginId){
        int row = teacherService.updateAll(teacher);
        if(row<1){
            model.addAttribute("error","修改时发生未知异常");
        }
        model.addAttribute("error","修改成功");
        model.addAttribute("colleges",collegeService.getColleges());
        model.addAttribute("teacher",teacherService.getTeacherById(loginId));
        return "teacher/admin/teacherUpdate";
    }

    @RequestMapping("toCollegeAndMajor")
    public String toCollegeAndMajor(Model model, String college,
                                    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        List<College> colleges = collegeService.getLikeColleges(college);
        //分页
        PageInfo pageInfo = new PageInfo(pageNum,colleges);
        model.addAttribute("pageInfo",pageInfo);
        return "teacher/admin/CollegeAndMajor";
    }

    @RequestMapping("toAddCollegeAndMajor")
    public String toAddCollegeAndMajor(){
        return "teacher/admin/addCollegeAndMajor";
    }

    @RequestMapping("addCollegeAndMajor")
    public String addCollegeAndMajor(Model model,String college,String[] major){
        try {
            List<Major> list = new ArrayList<>();
            collegeService.addColleges(college);
            for(String s:major){
                if(s.equals("") || s==null){
                    continue;
                }else {
                    list.add(new Major(college,s));
                }
            }
            majorService.addMajors(list);
        }catch (Exception e){
            model.addAttribute("error","添加时发生未知错误");
        }
        model.addAttribute("error","添加成功");
        return "teacher/admin/addCollegeAndMajor";
    }

    @RequestMapping("showCollege")
    public String showCollege(Model model,String college){
        List<Major> majors = majorService.getMajorsByCollege(college);
        model.addAttribute("college",college);
        model.addAttribute("majors",majors);
        return "teacher/admin/showCollege";
    }

    @RequestMapping("toUpdateCollege")
    public String toUpdateCollege(Model model,String college){
        College college1 = collegeService.getCollege(college);
        List<Major> majors = majorService.getMajorsByCollege(college);
        model.addAttribute("college",college1);
        model.addAttribute("majors",majors);
        return "teacher/admin/updateCollege";
    }

    @RequestMapping("updateCollege")
    public String updateCollege(Model model,Integer id,String college,String[] major){
        try{
            College college1 = collegeService.getCollegeById(id);
            College upCollege = new College();
            upCollege.setId(id);
            upCollege.setCollege(college);
            collegeService.updateCollege(upCollege);
            List<Major> list = new ArrayList<>();
            for(String s:major){
                if(s.equals("") || s==null){
                    continue;
                }else {
                    list.add(new Major(college,s));
                }
            }
            if(list!=null){
                int row = majorService.updateCollegeMajor(college1.getCollege(),list);
            }
        }catch(Exception e){
            model.addAttribute("error","修改时发生未知错误");
        }
        model.addAttribute("error","修改成功");
        return toUpdateCollege(model,college);
    }

    @RequestMapping("delCollege")
    public String delCollege(Model model,String college){
        try{
            collegeService.delCollege(college);
            majorService.delCollegeMajor(college);
        }catch (Exception e){
            model.addAttribute("error","删除时发生未知异常");
        }
        model.addAttribute("error","删除成功");
        return toCollegeAndMajor(model,"",1);
    }
}
